package com.cwn.wethink.common.utils.rar;

import net.sf.sevenzipjbinding.ExtractAskMode;
import net.sf.sevenzipjbinding.ExtractOperationResult;
import net.sf.sevenzipjbinding.IArchiveExtractCallback;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.ISequentialOutStream;
import net.sf.sevenzipjbinding.PropID;
import net.sf.sevenzipjbinding.SevenZipException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExtractCallback implements IArchiveExtractCallback {
 
    private int index;
    private IInArchive inArchive;
    private String ourDir;
    private List<String> nameList = new ArrayList<>();
 
    public ExtractCallback(IInArchive inArchive, String ourDir) {
        this.inArchive = inArchive;
        this.ourDir = ourDir;
    }
 
    @Override
    public ISequentialOutStream getStream(int index, ExtractAskMode extractAskMode)throws SevenZipException {
        this.index = index;
        final String path = (String) inArchive.getProperty(index, PropID.PATH);
        final boolean isFolder = (boolean) inArchive.getProperty(index,PropID.IS_FOLDER);
        final String[] oldPath = {""};
        long time = System.currentTimeMillis();
        return new ISequentialOutStream() {
            @Override
            public int write(byte[] data) throws SevenZipException {
                try {
                    if (!isFolder) {
                        File file = new File(ourDir +time+"_"+ path);
                        final String fileName = file.getName();
                        int i = nameList.indexOf(fileName);
                        if (i==-1){
                            nameList.add(fileName);
                        }
                        if (path.equals(oldPath[0])) {
                            save2File(file, data, true);
                        } else {
                            save2File(file, data, false);
                        }
                        oldPath[0] = path;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    nameList = new ArrayList<>();
                }
                return data.length;
            }
        };
    }
 
    @Override
    public void prepareOperation(ExtractAskMode extractAskMode) throws SevenZipException {
 
    }
 
    @Override
    public void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {
 
    }
 
    @Override
    public void setTotal(long l) throws SevenZipException {
 
    }
 
    @Override
    public void setCompleted(long l) throws SevenZipException {
 
    }
 
    public boolean save2File(File file, byte[] msg, boolean append) {
        FileOutputStream fos = null;
        try {
            File parent = file.getParentFile();
            boolean bool;
            if ((!parent.exists()) && (!parent.mkdirs())) {
                return false;
            }
            fos = new FileOutputStream(file, append);
            fos.write(msg);
            fos.flush();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
    public List<String> getNameList() {
        return nameList;
    }
 
    public void setNameList(List<String> nameList) {
        this.nameList = nameList;
    }
}